﻿using System.Runtime.CompilerServices;

namespace KinonekoSoftware.CodariaDB.Documents.TemplateContents
{
    public sealed class TemplatedContentEngine : _Delta
    {
        protected override void OnDatabaseOpening(RepositoryOpenContext context)
        {
            TemplatedDB = context.Repository.Get<TemplatedContent>(CollectionNames.TemplatedContentDB);
        }

        protected override void OnDatabaseClosing(ILogger logger)
        {
            TemplatedDB = null;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public T Get<T>(string id) where T : TemplatedContent => TemplatedDB.FindById(id) as T;

        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasTemplatedContent(string id) => TemplatedDB.HasID(id);



        public void AddTemplatedContent(TemplatedContent instance)
        {
            if (instance is null)
            {
                return;
            }

            Modified(instance);
            TemplatedDB.Upsert(instance);
        }

        public void UpdateTemplatedContent(TemplatedContent instance)
        {
            if (instance is null)
            {
                return;
            }

            Modified(instance);
            TemplatedDB.Update(instance);
        }

        public void RemoveTemplatedContent(string id) => TemplatedDB.Delete(id);

        public ILiteCollection<TemplatedContent> TemplatedDB { get; private set; }

        public override string EngineID => EngineNames.Templated;
    }
}